/*
Copyright 2017 yangchong211（github.com/yangchong211）

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.yc.video.player;

import ohos.aafwk.ability.Ability;
import ohos.agp.components.ComponentContainer;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.AbilityContext;
import ohos.app.Context;
import ohos.global.resource.BaseFileDescriptor;

import com.yc.video.controller.BaseVideoController;
import com.yc.video.tool.PlayerUtils;

/**
 * 播放器帮助类
 *
 * @since 2021-05-10
 */
public class VideoPlayerHelper {
    private static VideoPlayerHelper sInstance;

    /**
     * 构造方法，避免直接new
     */
    private VideoPlayerHelper() {
    }

    /**
     * 一定要使用单例模式，保证同一时刻只有一个视频在播放，其他的都是初始状态
     * 单例模式
     *
     * @return VideoPlayerManager对象
     */
    public static VideoPlayerHelper instance() {
        if (sInstance == null) {
            sInstance = new VideoPlayerHelper();
        }
        return sInstance;
    }

    /**
     * 获取DecorView
     *
     * @param context 上下文
     * @param videoController controller
     * @return return
     */
    protected ComponentContainer getDecorView(Context context, BaseVideoController videoController) {
        AbilityContext activity = VideoPlayerHelper.instance().getActivity(context, videoController);
        if (activity == null) {
            return null;
        }
        return null;
    }

    /**
     * 获取activity中的content view
     *
     * @param context 上下文
     * @param videoController controller
     * @return return
     */
    protected ComponentContainer getContentView(Context context, BaseVideoController videoController) {
        return null;
    }

    /**
     * 获取Activity，优先通过Controller去获取Activity
     *
     * @param context 上下文
     * @param videoController controller
     * @return return
     */
    protected Ability getActivity(Context context, BaseVideoController videoController) {
        Ability activity;
        if (videoController != null) {
            activity = PlayerUtils.scanForActivity(videoController.getContext());
            if (activity == null) {
                activity = PlayerUtils.scanForActivity(context);
            }
        } else {
            activity = PlayerUtils.scanForActivity(context);
        }
        return activity;
    }

    /**
     * 显示NavigationBar和StatusBar
     *
     * @param decorView decorView
     * @param context 上下文
     * @param videoController controller
     */
    protected void showSysBar(ComponentContainer decorView, Context context, BaseVideoController videoController) {
        VideoPlayerHelper.instance().getActivity(context, videoController)
            .getWindow()
            .clearFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
    }

    /**
     * 隐藏NavigationBar和StatusBar
     *
     * @param decorView decorView
     * @param context 上下文
     * @param videoController controller
     */
    protected void hideSysBar(ComponentContainer decorView, Context context, BaseVideoController videoController) {
        Window window = VideoPlayerHelper.instance().getActivity(context, videoController).getWindow();
        window.addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
    }

    /**
     * 判断是否为本地数据源，包括 本地文件、Asset、raw
     *
     * @param url url地址
     * @param assetFileDescriptor assets文件
     * @return 是否为本地数据源
     */
    protected boolean isLocalDataSource(String url, BaseFileDescriptor assetFileDescriptor) {
        if (assetFileDescriptor != null) {
            return true;
        }
        return false;
    }
}
